﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.UserSkins;
using DevExpress.XtraBars.Helpers;
using System.Data.SqlClient;
using QLTV.BLL;
using QLTV.Public;


namespace QLTV.GUI
{
    public partial class frmTraCuu : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        
        TheLoaiBLL tlBLL = new TheLoaiBLL();
        DauSachBLL dsBLL = new DauSachBLL();
        DauSachDTO dsDTO = new DauSachDTO();
        public frmTraCuu()
        {
            InitializeComponent();
        }

        private void frmTraCuu_Load(object sender, EventArgs e)
        {            
            //Nút đổi skin
            DevExpress.UserSkins.BonusSkins.Register();
            SkinHelper.InitSkinPopupMenu(barSkin);

            DataTable tb = tlBLL.LoadCoTenTheLoai();
            tb.Rows.Add("Tất Cả");

            cbTheLoai.DisplayMember = "TenTL";
            cbTheLoai.ValueMember = "MaTL";
            cbTheLoai.DataSource = tb;

            cbTheLoai.SelectedIndex = cbTheLoai.Items.Count - 1;
            cbCotTimKiem.Text = "Tất Cả";


            gridTraCuuSach.DataSource = dsBLL.DauSach_TraCuu();        
            
              
           
            
            
        }

        private void btnDangNhap_ItemClick(object sender, ItemClickEventArgs e)
        {
            Forms.frmDangNhap fDN = new Forms.frmDangNhap();

            fDN.myEvent += frm_myEvent;

            fDN.ShowDialog();
        }
        private void frm_myEvent(object sender, bool check)
        {
            bool kt = check;
            if (kt == true)
                this.Hide();
        }

        private void txtGiaTriTimKiem_EditValueChanged(object sender, EventArgs e)
        {

            if (cbCotTimKiem.Text == "Tất Cả")
            {
                if (cbTheLoai.SelectedIndex == cbTheLoai.Items.Count - 1)
                {
                    if (txtGiaTriTimKiem.Text.ToString().Trim().Length == 0)
                    {
                        gridTraCuuSach.DataSource = dsBLL.DauSach_TraCuu();
                    }
                    else
                    {
                        dsDTO.TenDS = txtGiaTriTimKiem.Text.ToString().Trim();
                        gridTraCuuSach.DataSource = dsBLL.timDauSachDayDu(dsDTO);
                    }
                }
                else
                {
                    if (txtGiaTriTimKiem.Text.ToString().Trim().Length == 0)
                    {
                        dsDTO.MaTL = cbTheLoai.Text;
                        gridTraCuuSach.DataSource = dsBLL.timDauSachTheoTheLoai(dsDTO);
                    }
                    else
                    {
                        dsDTO.MaTL = cbTheLoai.Text;
                        dsDTO.TenDS = txtGiaTriTimKiem.Text.ToString().Trim();
                        gridTraCuuSach.DataSource = dsBLL.timDauSachTheoTheLoai_DayDu(dsDTO);
                    }
                }
            }
            if (cbCotTimKiem.Text == "Tên Đầu Sách")
            {
                if (cbTheLoai.SelectedIndex != cbTheLoai.Items.Count - 1)
                {
                    if (txtGiaTriTimKiem.Text.ToString().Trim().Length == 0)
                    {
                        dsDTO.MaTL = cbTheLoai.Text;
                        gridTraCuuSach.DataSource = dsBLL.timDauSachTheoTheLoai(dsDTO);
                    }
                    else
                    {
                        dsDTO.MaTL = cbTheLoai.Text;
                        dsDTO.TenDS = txtGiaTriTimKiem.Text.ToString().Trim();
                        gridTraCuuSach.DataSource = dsBLL.timDauSachTheoTheLoai_TenDS(dsDTO);
                    }
                }
                else
                {
                    if (txtGiaTriTimKiem.Text.ToString().Trim().Length == 0)
                    {
                        gridTraCuuSach.DataSource = dsBLL.DauSach_TraCuu();
                    }
                    else
                    {
                        dsDTO.TenDS = txtGiaTriTimKiem.Text.ToString().Trim();
                        gridTraCuuSach.DataSource = dsBLL.timDauSachDayDu_TenDS(dsDTO);
                    }
                }
            }
            if (cbCotTimKiem.Text == "Tác Giả")
            {
                if (cbTheLoai.SelectedIndex != cbTheLoai.Items.Count - 1)
                {
                    if (txtGiaTriTimKiem.Text.ToString().Trim().Length == 0)
                    {
                        dsDTO.MaTL = cbTheLoai.Text;
                        gridTraCuuSach.DataSource = dsBLL.timDauSachTheoTheLoai(dsDTO);
                    }
                    else
                    {
                        dsDTO.MaTL = cbTheLoai.Text;
                        dsDTO.MaTG = txtGiaTriTimKiem.Text.ToString().Trim();
                        gridTraCuuSach.DataSource = dsBLL.timDauSachTheoTheLoai_TenTG(dsDTO);
                    }
                }
                else
                {
                    if (txtGiaTriTimKiem.Text.ToString().Trim().Length == 0)
                    {
                        gridTraCuuSach.DataSource = dsBLL.DauSach_TraCuu();
                    }
                    else
                    {
                        dsDTO.MaTG = txtGiaTriTimKiem.Text.ToString().Trim();
                        gridTraCuuSach.DataSource = dsBLL.timDauSachDayDu_TenTG(dsDTO);
                    }
                }
            }
            if (cbCotTimKiem.Text == "Nhà Xuất Bản")
            {
                if (cbTheLoai.SelectedIndex != cbTheLoai.Items.Count - 1)
                {
                    if (txtGiaTriTimKiem.Text.ToString().Trim().Length == 0)
                    {
                        dsDTO.MaTL = cbTheLoai.Text;
                        gridTraCuuSach.DataSource = dsBLL.timDauSachTheoTheLoai(dsDTO);
                    }
                    else
                    {
                        dsDTO.MaTL = cbTheLoai.Text;
                        dsDTO.MaNXB = txtGiaTriTimKiem.Text.ToString().Trim();
                        gridTraCuuSach.DataSource = dsBLL.timDauSachTheoTheLoai_TenNXB(dsDTO);
                    }
                }
                else
                {
                    if (txtGiaTriTimKiem.Text.ToString().Trim().Length == 0)
                    {
                        gridTraCuuSach.DataSource = dsBLL.DauSach_TraCuu();
                    }
                    else
                    {
                        dsDTO.MaNXB = txtGiaTriTimKiem.Text.ToString().Trim();
                        gridTraCuuSach.DataSource = dsBLL.timDauSachDayDu_TenNXB(dsDTO);
                    }
                }
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            txtGiaTriTimKiem.Text = null;
            if ((txtGiaTriTimKiem.Text.ToString().Trim().Length == 0) && (cbTheLoai.SelectedIndex == cbTheLoai.Items.Count - 1))
            {
                gridTraCuuSach.DataSource = dsBLL.DauSach_TraCuu();
            }
            if (cbTheLoai.SelectedIndex != cbTheLoai.Items.Count - 1)
            {
                dsDTO.MaTL = cbTheLoai.Text;
                gridTraCuuSach.DataSource = dsBLL.timDauSachTheoTheLoai(dsDTO);
            }
        }       

        private void cbTheLoai_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbTheLoai.SelectedIndex == cbTheLoai.Items.Count - 1)
            {
                gridTraCuuSach.DataSource = dsBLL.DauSach_TraCuu();
            }
            else
            {
                dsDTO.MaTL = cbTheLoai.Text;
                gridTraCuuSach.DataSource = dsBLL.timDauSachTheoTheLoai(dsDTO);
            }
        }

     
    }
}
